<%@ page language="java" isELIgnored="false" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%@ include file="/WEB-INF/jsp/ResourceGlobal.jsp"%>
<html>
	<head>   
		<!-- 这里引入导航信息header.tpl -->
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
		<title>修改密码</title>
		<style type="text/css">
			.container {
			    padding-right: 20px;
			    padding-left: 20px;
			    margin-right: auto;
			    margin-left: auto;
			}
		</style>
	</head>
	<body class="sub-page container">
		<form id="pwdModifyForm" name="pwdModifyForm" class="form-horizontal" method="post" style="margin-top: 15px;">
			<div class="model-content">
				<div class="form-group">
	            	<label class="col-sm-2 control-label">旧密码<span class="text-danger">*</span><label class="error" for="oldPassword"></label></label>
		            <div class="col-sm-10 form-inner">
	 					<input class="form-control" placeholder="旧密码" type="password" id="oldPassword" name="oldPassword">
					</div>
		        </div>
				<div class="form-group">
	            	<label class="col-sm-2 control-label">新密码<span class="text-danger">*</span><label class="error" for="newPassword"></label></label>
		            <div class="col-sm-10 form-inner">
	 					<input class="form-control" placeholder="新密码" type="password" id="newPassword" name="newPassword">
					</div>
		        </div>
				<div class="form-group">
	            	<label class="col-sm-2 control-label">确认新密码<span class="text-danger">*</span><label class="error" for="confirmPassword"></label></label>
		            <div class="col-sm-10 form-inner">
	 					<input class="form-control" placeholder="确认新密码" type="password" id="confirmPassword" name="confirmPassword">
					</div>
		        </div>
			</div>
			
			<div class="page-footer" >
				<button type="button" id="submitBtn" class="btn btn-primary btn-save btn-loading btn-w-xs" data-loading-text="<i class='ico-loading'></i>" onclick="pwdModify.submitForm(this)">修改</button>
				<button type="button" id="cancelBtn" class="btn btn-link btn-w-xs" onclick="pwdModify.cancel()">取消</button>
			</div>
		</form>
		
		<script type="text/javascript">
		
			var PWD_MODIFY = function(){
				this.init();
			};
			/**
			 * 初始化方法
			 */
			PWD_MODIFY.prototype.init = function(){
				var self = this;
				self.expand();
				
				$("#pwdModifyForm").validate({
					focusCleanup : true
				});
				
				self.validate();
				
				// 尽量减少后台请求
				$("#oldPassword").focus(function() {
					$("#oldPassword").rules("remove");
					$("#oldPassword").rules("add", {
						required : true, messages: {required: "请填写旧密码"}
					});
				});
			}

			/**
			 * 拓展方法
			 */
			PWD_MODIFY.prototype.expand = function(){
				var self = this;
				
				self.validate = function() {
					$("#oldPassword").rules("remove");
					$("#newPassword").rules("remove");
					$("#confirmPassword").rules("remove");
					
					$("#oldPassword").rules("add", {
						required : true, messages: {required: "请填写旧密码"}
					});
					$("#newPassword").rules("add", {
						required : true, sPassword: true, equalsOld: true, messages: {required: "请填写新密码"}
					});
					$("#confirmPassword").rules("add", {
						required : true, comparePassword: true , messages: {required: "请确认新密码"}
					});
				};
				self.cancel = function() {
					var index = parent.layer.getFrameIndex(window.name);  
				    parent.layer.close(index);
				};
				self.submitForm = function(targetx) {
					
					$(targetx).button('loading');
					
					self.validate();
					
					$("#oldPassword").rules("remove");
					$("#oldPassword").rules("add", {
						required : true, oldPassword: true, messages: {required: "请填写旧密码"}
					});
					
					if(!$("form[name='pwdModifyForm']").valid()) {
						
   						$(targetx).button('reset');
						
						return false;
					}
					
					layer.confirm('确认修改密码？成功后请重新登录', {
						icon: 6,
						skin: 'layer-ext-moon',
						btn: ['确认','取消'],
						isOutAnim: false
					}, function() {
						$.ajax({  
				            type : "POST",  //提交方式  
				            url : '/scbp/sys/user/modifyPassword.do',  
				            data : {empId: current_id, login: current_login, password: $("#newPassword").val()},  
				            dataType: 'json',
				            success : function(data) {//返回数据根据结果进行相应的处理  
				            	if(data) {
				    				if(data.success) {
				    					logout();
				    				} else {
				    					setTimeout(function() {
				    						toastr.warning('', data.message);
				    						$(targetx).button('reset');
				    					}, 1000);
				    				}
				    			} else {
				    				// 正常不可能走这里
				    				setTimeout(function() {
				    					toastr.warning('', '修改失败，后台异常');
				    					$(targetx).button('reset');
				    				}, 1000);
				    			}
				            },
				            error: function() {
				            	setTimeout(function() {
				    				toastr.warning('', '修改失败，后台异常');
				    				$(targetx).button('reset');
				    			}, 1000);
				            }
				        });
					}, function() {
	    				$(targetx).button('reset');
					});
				};
			}
			
			jQuery.validator.addMethod("equalsOld", function(value, element, param) { 
				var oldPassword = $("#oldPassword").val();
				
			    var flag = oldPassword == value;
			    
			    if(flag) {
			    	jQuery.validator.messages.equalsOld = "新密码不能和旧密码一样";
			    	return false;
			    }
			    return true;
			});
			jQuery.validator.addMethod("comparePassword", function(value, element, param) { 
				
				var newPassword = $("#newPassword").val();
				
			    var flag = newPassword == value;
			    
			    if(!flag) {
			    	jQuery.validator.messages.comparePassword = "两次输入密码不一致";
			    	return false;
			    }
			    return true;
			});
			
			jQuery.validator.addMethod("oldPassword", function(value, element, param) { 
			    $.ajax({
					  url: "/scbp/sys/user/checkOldPassword.do",
					  dataType: "json",
					  type: "POST",
					  cache: false,
					  async: false,
					  data: {
						  empId: current_id,
						  password : value
					  },
					  success: function(data) {
							if(!data.success){
								flag = false;
								return;
							}
							flag = true;
					  },
					  error: function(jqXHR, textStatus, errorThrown){
							ctools.alert("查询失败" + textStatus);
							flag = false;
					  }
				});
			    if(!flag) {
			    	jQuery.validator.messages.oldPassword = "旧密码输入有误";
			    }
			    return flag;
			});
			
			$(function() {
				window.pwdModify = new PWD_MODIFY();
			});
		</script>
	</body>
</html>